package com.onehouse.domain.room.service;

import com.onehouse.domain.room.model.RoomEntity;
import com.onehouse.domain.room.model.RoomStatus;
import com.onehouse.domain.room.repository.IRoomRepository;
import com.onehouse.domain.room.repository.IRoomTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomService implements IRoomService {
    @Autowired
    private IRoomRepository roomRepository;

    @Autowired
    private IRoomTypeRepository roomTypeRepository;

    public List<RoomEntity> listAllRoom() {
        return roomRepository.selectList();
    }

    public void createRooms(Long typeId, List<String> roomNumbers) {
        // 验证房间类型是否存在
        if (!roomTypeRepository.selectById(typeId).isPresent()) {
            throw new IllegalArgumentException("Room type does not exist");
        }

        // 验证房间号是否已存在
        for (String roomNumber : roomNumbers) {
            if (roomRepository.selectByRoomNumber(roomNumber).isPresent()) {
                throw new IllegalArgumentException("Room number already exists: " + roomNumber);
            }
        }

        // 批量创建房间
        for (String roomNumber : roomNumbers) {
            RoomEntity room = RoomEntity.builder()
                    .roomTypeId(typeId)
                    .roomNumber(roomNumber)
                    .status(RoomStatus.VACANT)
                    .isDeleted(false)
                    .build();
            roomRepository.insert(room);
        }
    }

    public void updateRoom(RoomEntity room) {
        // 验证房间是否存在
        if (!roomRepository.selectByRoomNumber(room.getRoomNumber()).isPresent()) {
            throw new IllegalArgumentException("Room does not exist");
        }
        roomRepository.update(room);
    }

    public void updateRooms(List<RoomEntity> rooms) {
        // 收集不存在的房间号
        List<String> nonExistentRooms = new ArrayList<>();
        for (RoomEntity room : rooms) {
            if (!roomRepository.selectByRoomNumber(room.getRoomNumber()).isPresent()) {
                nonExistentRooms.add(room.getRoomNumber());
            }
        }

        // 如果有不存在的房间，抛出异常并列出所有不存在的房间号
        if (!nonExistentRooms.isEmpty()) {
            throw new IllegalArgumentException("Rooms do not exist: " + String.join(", ", nonExistentRooms));
        }

        // 批量更新房间
        for (RoomEntity room : rooms) {
            roomRepository.update(room);
        }
    }

    public void deleteRoomsByType(Long typeId) {
        // 验证房间类型是否存在
        if (!roomTypeRepository.selectById(typeId).isPresent()) {
            throw new IllegalArgumentException("Room type does not exist");
        }

        // 获取该类型的所有房间并验证状态
        List<RoomEntity> rooms = roomRepository.selectList();
        for (RoomEntity room : rooms) {
            if (room.getRoomTypeId().equals(typeId)) {
                if (room.getStatus() != RoomStatus.VACANT) {
                    throw new IllegalStateException("Cannot delete room that is not vacant: " + room.getRoomNumber());
                }
                roomRepository.deleteById(room.getRoomId());
            }
        }
    }

    public void deleteRooms(List<String> roomNubers) {
        // 收集不存在的房间号
        List<String> nonExistentRooms = new ArrayList<>();
        for (String roomNumber : roomNubers) {
            if (!roomRepository.selectByRoomNumber(roomNumber).isPresent()) {
                nonExistentRooms.add(roomNumber);
            }
        }

        // 如果有不存在的房间，抛出异常并列出所有不存在的房间号
        if (!nonExistentRooms.isEmpty()) {
            throw new IllegalArgumentException("Rooms do not exist: " + String.join(", ", nonExistentRooms));
        }

        // 批量删除房间
        for (String roomNumber : roomNubers) {
            roomRepository.deleteByNumber(roomNumber);
        }
    }
}
